/* 
 * Authors: Edouard Eynard
 *          Jolan Houvenaeghel
 *          Tanguy Falconnet
 *
 * Date :	11/03/2014
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "fonction.h"

int isThereLoops(){
	char **tab;
	int size=1, i;
	int answer;
	for (i = 0; i < sizeTab; i++) {
		tab = malloc(sizeof(char**));
		tab[0] = malloc(sizeof(char*));
		strcpy(tab[0],tabPerson[i]->name);
		answer = recursive(tab,size,i);	
		
		if(answer == -1){
			printf("Irrégularité dans l'arbre de %s\n",tab[0]);
			return -1;
		}else{
			printf("Arbre de %s correct\n",tab[0]);
		}
		free(tab);
	}
	return 0;
}

int recursive(char **tab, int size, int index){
	int i=0,j,k,z;
	int answer = 0;
	char **tabRec;
	char * src;
	tabRec = malloc(sizeof(char*)*size+1);
	for(i=0;i<size;i++){
		tabRec[i] = malloc(strlen(tab[i])*sizeof(char));
		strcpy(tabRec[i],tab[i]);
	}
	i=0;
	size++;

	if (tabPerson[index]->dad != NULL) {
		tabRec[size-1] = malloc(strlen(tabPerson[index]->dad->name)*sizeof(char));
		strcpy(tabRec[size-1],tabPerson[index]->dad->name);
		for(j=0;j<size-1;j++){
			for(k=j+1;k<size;k++){
				if(strcmp(tabRec[j],tabRec[k]) == 0){
					return -1;
				}
			}
		}

		answer = recursive(tabRec, size, indexOf(tabPerson[index]->dad->name));
		if(answer == -1){
			return answer;
		}
	}
	if (tabPerson[index]->mum != NULL) {
		tabRec[size-1] = malloc(strlen(tabPerson[index]->mum->name)*sizeof(char));
		strcpy(tabRec[size-1],tabPerson[index]->mum->name);
		for(j=0;j<size-1;j++){
			for(k=j+1;k<size;k++){
				if(strcmp(tabRec[j],tabRec[k]) == 0){
					return -1;
				}
			}
		}

		answer = recursive(tabRec, size, indexOf(tabPerson[index]->mum->name));
		if(answer == -1){
			return answer;
		}
	}
	return answer;
}

int indexOf(char * name){
	int i;
	for(i=0;i<sizeTab;i++){
		if(equal(name,tabPerson[i]->name)==0){
			return i;
		}
	}
	return -1;
}

int equal(char *c1, char* c2){
	int i=0;
	if(strcmp(c1,c2)!=0){
		return -1;
	}
	while(i<=strlen(c1)){
		if(c1[i]!=c2[i]){
			return -1;
		}
		i++;
	}
	return 0;
}

char** splitLine(char* line){
	int i,nbWords=0;
	char * word;
	char * buffer;
	char ** tab;
	buffer = strdup(line);
	word = strtok(buffer,";");
	tab = malloc(sizeof(char**));
	tab[nbWords] = malloc(sizeof(char*));
	while(word != NULL){
		strcpy(tab[nbWords],word);
		nbWords++;
		tab = realloc(tab, (nbWords+1) * sizeof(char**));
		tab[nbWords] = malloc(sizeof(char*));
		word = strtok(NULL,";");
	}
	return tab;
}

void displayString(char * string){
	printf("%s\n",string);
}

void debug(int line){
	printf("%d\n",line);
}

void createTree(){
	int i,j;
	char line[TAILLE_MAX];
	char ** names;
	int person, mum, dad;
	FILE * file = fopen("genealogie.txt", "r");
	if(file == NULL){
		printf("Echec accès fichier\n");
	}
	while(fgets(line, TAILLE_MAX, file) != NULL){
		names = splitLine(line);
		person = addPerson(names[PERSON]);
		dad = addPerson(names[DAD]);
		mum = addPerson(names[MUM]);
		addParents(person, tabPerson[dad], tabPerson[mum]);
	}
	fclose(file);
	
}

int addPerson(char * name){
	int index;
	index = indexOf(name);

	if(index != -1){
		return index;
	}
	
	if(sizeTab > 0){
		tabPerson = realloc(tabPerson, sizeof(Person*)*(sizeTab+1));
	} else {
		tabPerson = malloc(sizeof(Person*));
	}
	tabPerson[sizeTab] = malloc(sizeof(Person));
	tabPerson[sizeTab]->dad = NULL;
	tabPerson[sizeTab]->mum = NULL;
	
	tabPerson[sizeTab]->name = malloc(strlen(name)*sizeof(char));
	strcpy(tabPerson[sizeTab]->name,name);
	sizeTab++;
	return (sizeTab-1);
}

int addParents(int index, Person *dad, Person *mum){
 	tabPerson[index]->dad = dad;
	tabPerson[index]->mum = mum;
}

void displayTree(){
	int i;
	printf("\nArbre :\n");
	for(i=0;i<sizeTab;i++){
		printf("Nom : %s",tabPerson[i]->name);
		if(tabPerson[i]->dad != NULL){
			printf(", Père : %s",tabPerson[i]->dad->name);
		}
		if(tabPerson[i]->mum != NULL){
			printf(", Mère : %s",tabPerson[i]->mum->name);
		}
		printf("\n");
	}
	printf("\n\n");
}

void init(){
	sizeTab=0;
	createTree();
	displayTree();
	isThereLoops();
}
